import org.junit.Test;

import java.io.*;

public class testIO {
    @Test
    public void inputFile01() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("frankbase02/file/IOfile");
        int by;
        String string = new String();
        while ((by = fileInputStream.read()) != -1) {
            //read()读一个字符以ascll显示，并且返回一个int 类型
            // 所以需要接受，要不然少数据
            System.out.print(by + " ");
            string = string + (char) by;
        }
        fileInputStream.close();
        System.out.println(string);
    }

    @Test
    public void outputFile01() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("frankbase02/file/love.txt");
        byte[] bytes = "i love you xhl".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            fileOutputStream.write((char) bytes[i]);//一个字节一个字节的去写入
        }
    }

    @Test
    public void copyPicture() throws IOException {//copy picture
        FileInputStream fileInputStream = new FileInputStream("frankbase02/file/1.png");
        FileOutputStream fileOutputStream = new FileOutputStream("frankbase02/file/target.png");
        byte[] bytes = new byte[1024];
        int by;
        while ((by = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, by);
        }
        fileInputStream.close();
        ;
        fileOutputStream.close();
    }

    @Test
    public void copyPicture01() throws IOException {//copying picture use   BufferedInputStream and BufferedOutputStream
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("frankbase02/file/1.png"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("frankbase02/file/result.png"));
        int by;
        while ((by = bufferedInputStream.read()) != -1) {
            bufferedOutputStream.write(by);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

    @Test
    public void inAndOutFile() throws IOException {
        FileReader fileReader = new FileReader("frankbase02/file/love.txt");
        FileWriter fileWriter = new FileWriter("frankbase02/file/love01.txt");
        int ch;
        while ((ch = fileReader.read()) != -1) {
            System.out.print((char) ch);
        }
        fileWriter.write("xhl i love you");
        fileReader.close();
        fileWriter.close();
    }

    @Test
    /*
     * 缓冲区如果没有填满，需要手动关闭才能把数据从缓冲区传入input文件中去
     */

    public void bufferWritterTest() throws IOException {

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("frankbase02/file/love02.txt"));
        BufferedReader bufferedReader = new BufferedReader(new FileReader("frankbase02/file/love.txt"));
        String string;
        while ((string = bufferedReader.readLine()) != null) {
            bufferedWriter.write(string + "\n");
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
}
